<template>
  <div class="ele-body">
    <a-row :gutter="12">
      <a-col :span="24">
        <div class="ten-height bg-white padding10 box-shadow">
          <div class="table-header">
            <div class="header-left">
              <a-space size="large">
                <a-input
                  style="width: 300px"
                  v-model:value="where.searchText"
                  placeholder="日志内容 (回车搜索)"
                  @pressEnter="search"
                  class="search-input"
                >
                  <template #suffix>
                    <search-outlined @click="search" />
                  </template>
                </a-input>

                <a-button type="primary" danger @click="clearSearch">清空条件</a-button>
                <a-button @click="callbackUp">返回</a-button>
              </a-space>
            </div>
            <div class="header-right">
              <a-space> </a-space>
            </div>
          </div>
          <div class="table-content">
            <common-table
              :columns="columns"
              :where="where"
              :rowSelection="false"
              bordered
              :pageSize="100"
              :isInit="false"
              ref="tableRef"
              url="/sysLogBusinessContent/page"
              :scroll="{ y: 'calc(100vh - 240px)' }"
            >
              <template #bodyCell="{ column, record }">
                <template v-if="column.dataIndex == 'logContent'">
                  <div v-html="getContent(record.logContent)"></div>
                </template>
              </template>
            </common-table>
          </div>
        </div>
      </a-col>
    </a-row>
  </div>
</template>

<script setup name="BusinessLogDetail">
import { onMounted, ref } from 'vue';

const emits = defineEmits(['updateType']);

const props = defineProps({
  // 业务日志id
  businessLogId: {
    type: String
  }
});

// table dom
const tableRef = ref(null);

// 表格字段
const columns = ref([
  {
    title: '序号',
    isShow: true,
    width: 50
  },
  {
    title: '日志记录内容',
    isShow: true,
    dataIndex: 'logContent'
  }
]);

// 搜索条件
const where = ref({
  businessLogId: '',
  searchText: ''
});

/**
 * 搜索
 */
const search = () => {
  tableRef.value.reload();
};

// 获取内容
const getContent = content => {
  let data = '';
  if (content) {
    data = content.replace(new RegExp('\n', 'g'), '<br>');
  }
  return data;
};

/**
 * 清空搜索
 */
const clearSearch = () => {
  where.value.searchText = '';
  search();
};

/**
 * 返回
 */
const callbackUp = () => {
  emits('updateType', { type: 'list', businessLogId: null });
};

onMounted(() => {
  where.value.businessLogId = props.businessLogId;
  where.value.searchText = '';
  search();

  document.addEventListener('keydown', function (event) {
    // 判断按下的按键是否是 Backspace 键
    if (event.key === 'Backspace') {
      // 判断焦点是否在输入框中
      const activeElement = document.activeElement;
      const isInputFocused = activeElement && activeElement.tagName === 'INPUT';

      if (!isInputFocused) {
        // 在这里执行 Backspace 按键被按下时的操作
        callbackUp();
      }
    }
  });
});
</script>

<style scoped lang="less">
.table-record-title {
  cursor: pointer;
  color: var(--primary-color);
}

.table-header {
  width: 100%;
  height: 40px;
  margin-bottom: 10px;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.table-content {
  width: 100%;
  height: calc(100% - 50px);
}

:deep(.ant-table-body) {
  min-height: calc(100vh - 240px) !important;
}
</style>
